//
//  TestCollectionViewWaterFlowLayout.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2023/2/27.
//  Copyright © 2023 moon. All rights reserved.
//

#import "TestCollectionViewWaterFlowLayout.h"


@implementation TestItemsCollectionViewModel
- (void)setSelectedCellModel:(MenuCollectionViewCellModel *)selectedCellModel{
    if(_selectedCellModel != selectedCellModel){
        _selectedCellModel.selected = NO;
        [_selectedCellModel refresh];
        _selectedCellModel = selectedCellModel;
        _selectedCellModel.selected = YES;
        [_selectedCellModel refresh];
        if(_selectedCellModel){
            self.selectedSectionModel = _selectedCellModel.sectionModel;
        }
    }
}
- (void)setSelectedSectionModel:(TestItemsSectionModel *)selectedSectionModel{
    if(_selectedSectionModel != selectedSectionModel){
        TestItemsSectionModel *old = _selectedSectionModel;
        _selectedSectionModel = selectedSectionModel;
        [self.collectionView performBatchUpdates:^{
            [old refresh];
            [_selectedSectionModel refresh];
        } completion:nil];
        if(self.selectedCellModel!=nil && self.selectedCellModel.sectionModel!=selectedSectionModel){
            if(_selectedSectionModel.numberOfCells>0){
                self.selectedCellModel = [_selectedSectionModel cellModelAtIndex:0];
            }
        }
    }
}
@end

@implementation TestItemsSectionModel
- (id)initWithHead:(NSString *)head foot:(NSString *)foot{
    if(self=[self init]){
        self.showHead = head.length>0;
        self.headTitle = head;
        self.headClass = [TestItemsSectionView class];
        
        self.showFoot = foot.length>0;
        self.footTitle = foot;
        self.footClass = [TestItemsSectionView class];
        
        self.maxLine = 2;
        self.lastLineItemCellClass = [TestItemLastLineItemView class];
        
        self.sectionInsets = UIEdgeInsetsMake(0, 10, 0, 20);
    }
    return self;
}
@end

@implementation TestItemCollectionViewCell
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.clipsToBounds = YES;
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        self.titleLabel.numberOfLines = 1;
        self.titleLabel.textColor = [UIColor mk_colorWithLight:[UIColor colorWithRed:0 green:0.5 blue:1 alpha:1]];
        [self.contentView addSubview:self.titleLabel];
        //
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[self.titleLabel] constraintParam:(MKUIFlowLayoutConstraintParam_H_C_C) contentInsets:MKUIEdgeInsetsMakeSameEdge(5) interitemSpacing:5];
        self.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.layer.borderWidth = 1;
        
        [self.contentView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(__onTap)]];
    }
    return self;
}
- (void)__onTap{
    [self.collectionCellModel didClickSelf];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)sizeThatFits:(CGSize)size{
    if(self.titleLabel.text.length==0){
        return CGSizeZero;
    }
    CGSize s = [self.flowlayout sizeThatFits:size resizeItems:YES];
    UIEdgeInsets insets = self.flowlayout.contentInsets;
    CGFloat fontSize = 14;
    CGSize minSize = CGSizeMake(fontSize+insets.left+insets.right, fontSize+insets.top+insets.bottom);
    s.width = MAX(MIN(size.width,s.width),minSize.width);
    s.height = MAX(MIN(size.height,s.height),minSize.height);
    return s;
}
- (void)setCollectionCellModel:(__kindof MKUICollectionViewCellModel *)collectionCellModel{
    [super setCollectionCellModel:collectionCellModel];
    self.titleLabel.text = self.customCellModel.modelObject.title;
    BOOL selected = self.customCellModel.selected;
    self.titleLabel.textColor = selected?[UIColor redColor]:[UIColor colorWithRed:0 green:0.5 blue:1 alpha:1];
    
    NSInteger index = self.collectionCellModel.indexInSectionModel;
    if(index<=1){
        self.contentView.backgroundColor = [UIColor lightGrayColor];
    }else{
        self.contentView.backgroundColor = [UIColor clearColor];
    }
}
MKDEF_SINGLETON(TestItemCollectionViewCell)
+ (CGSize)sizeWithCollectionView:(UICollectionView *)collectionView collectionCellModel:(__kindof MKUICollectionViewCellModel *)collectionCellModel fits:(CGSize)fitsSize{
    return [self dynamicSizeWithCollectionView:collectionView collectionCellModel:collectionCellModel cellShareInstance:[self sharedInstance] calBlock:^CGSize(UICollectionView * _Nonnull collectionView, MKUICollectionViewCellModel * _Nonnull cellModel, TestItemCollectionViewCell * _Nonnull cell) {
        return [cell sizeThatFits:fitsSize];
    }];
}
@end
@implementation TestItemLastLineItemView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.clipsToBounds = YES;
        self.expendButton = [[TestFunButton alloc] initWithTitle:@"v"];
        [self.expendButton setTitle:@"^" forState:UIControlStateSelected];
        [self.expendButton mk_addClickActionBlock:^(TestItemLastLineItemView *self) {
            self.expendButton.selected = !self.expendButton.selected;
            self.itemsSectionModel.expends = self.expendButton.selected;
            [self.itemsSectionModel refresh];
            TestItemsCollectionViewModel *model = self.sectionModel.collectionModel;
            if(model.whenDataChange){
                model.whenDataChange();
            }
        } context:self];
        [self.contentView addSubview:self.expendButton];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.expendButton.frame = bounds;
}
- (CGSize)sizeThatFits:(CGSize)size{
    return [self.expendButton sizeThatFits:size];
}
- (void)setCollectionSectionModel:(__kindof MKUICollectionViewSectionModel *)sectionModel forKind:(NSString *)kind{
    [super setCollectionSectionModel:sectionModel forKind:kind];
    self.itemsSectionModel = sectionModel;
    self.expendButton.selected = self.itemsSectionModel.expends;
}
MKDEF_SINGLETON(TestItemLastLineItemView)
+ (nullable MKUICollectionViewWaterFlowLayoutLastLineItemOption *)referenceOptionForLastLineItemWithCollectionView:(UICollectionView *)collectionView collectionSectionModel:(__kindof MKUICollectionViewSectionModel *)sectionModel forKind:(NSString *)kind{
    TestItemsSectionModel *sm = sectionModel;
    MKUICollectionViewWaterFlowLayoutLastLineItemOption *option = [[MKUICollectionViewWaterFlowLayoutLastLineItemOption alloc] init];
    option.showLastLineItemWithinMaxLine = NO;
    if(sm.expends){
        option.maxLines = 99999999;
    }else{
        option.maxLines = sm.maxLine;
    }
    option.showLastLineItemWithinMaxLine = sm.showLastLineItemWithinMaxLine;
    
    //    option.lastLineItemSize = CGSizeMake(44, 44);
    option.lastLineItemSize = [self dynamicReferenceSizeWithCollectionView:collectionView collectionSectionModel:sectionModel forKind:kind viewShareInstance:[self sharedInstance] calBlock:^CGSize(UICollectionView * _Nonnull collectionView, MKUICollectionViewSectionModel * _Nonnull sectionModel, NSString * _Nonnull kind, TestItemLastLineItemView * view) {
        return [view sizeThatFits:collectionView.bounds.size];
    }];
    return option;
}
@end

@implementation TestItemsSectionView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.deleteButton = [[TestFunButton alloc] initWithTitle:@"删除"];
        [self.deleteButton mk_addClickActionBlock:^(TestItemsSectionView *self) {
            TestItemsCollectionViewModel *model = self.sectionModel.collectionModel;
            [self.sectionModel.collectionModel removeSectionModel:self.sectionModel animated:YES completion:nil];
            if(model.whenDataChange){
                model.whenDataChange();
            }
        } context:self];
        [self.contentView addSubview:self.deleteButton];
        
        self.addButton = [[TestFunButton alloc] initWithTitle:@"添加"];
        [self.addButton mk_addClickActionBlock:^(TestItemsSectionView *self) {
            [self addSM];
        } context:self];
        [self.contentView addSubview:self.addButton];
        
        //
        self.flowlayout = [[MKUISegmentFlowLayoutConstraint alloc] initWithItems:@[self.textLabel,self.addButton,self.deleteButton] constraintParam:(MKUISegmentFlowLayoutConstraint_H_C) contentInsets:self.class.contentInsets interitemSpacing:10];
        self.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.layer.borderWidth = 1;
        [self.contentView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(__onTap)]];
        
        TestItemsCollectionViewModel *model = self.sectionModel.collectionModel;
        BOOL selected = self.sectionModel==model.selectedSectionModel;
        self.textLabel.textColor = (selected?[UIColor redColor]:[UIColor lightGrayColor]);
    }
    return self;
}
- (void)addSM{
    NSInteger sectionIndex = self.itemsSectionModel.indexInModel;
    
    if([self.kind isEqualToString:UICollectionElementKindSectionFooter]){
        sectionIndex++;
    }
    TestItemsSectionModel *sm = [[TestItemsSectionModel alloc] initWithHead:[NSString stringWithFormat:@"分组头部%@",@(sectionIndex)] foot:[NSString stringWithFormat:@"分组尾部%@",@(sectionIndex)]];
    if([self.kind isEqualToString:UICollectionElementKindSectionFooter]){
        sm.showHead = YES;
        sm.showFoot = YES;
    }else{
        sm.showHead = YES;
        sm.showFoot = YES;
    }
    sm.maxLine = sectionIndex+2;
    sm.showLastLineItemWithinMaxLine = YES;
    [self.sectionModel.collectionModel insertSectionModel:sm atIndex:sectionIndex animated:YES completion:nil];
    TestItemsCollectionViewModel *model = self.sectionModel.collectionModel;
    if(model.whenDataChange){
        model.whenDataChange();
    }
}
- (void)__onTap{
    TestItemsCollectionViewModel *model = self.sectionModel.collectionModel;
    model.selectedSectionModel = self.sectionModel;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize s = [self.flowlayout sizeThatFits:size resizeItems:YES];
    return s;
}
- (void)setCollectionSectionModel:(__kindof MKUICollectionViewSectionModel *)sectionModel forKind:(NSString *)kind{
    [super setCollectionSectionModel:sectionModel forKind:kind];
    self.itemsSectionModel = sectionModel;
    
    TestItemsCollectionViewModel *model = self.sectionModel.collectionModel;
    BOOL selected = self.sectionModel==model.selectedSectionModel;
    self.textLabel.textColor = (selected?[UIColor redColor]:[UIColor lightGrayColor]);
}
MKDEF_SINGLETON(TestItemsSectionView)
+ (CGSize)referenceSizeWithCollectionView:(UICollectionView *)collectionView collectionSectionModel:(MKUICollectionViewSectionModel *)sectionModel forKind:(NSString *)kind{
    return [self dynamicReferenceSizeWithCollectionView:collectionView collectionSectionModel:sectionModel forKind:kind viewShareInstance:[self sharedInstance] calBlock:^CGSize(UICollectionView * _Nonnull collectionView, MKUICollectionViewSectionModel * _Nonnull sectionModel, NSString * _Nonnull kind, TestItemsSectionView * _Nonnull view) {
        CGRect bounds = collectionView.mk_contentBounds;
        CGSize size = bounds.size;
        if(collectionView.mk_collectionViewWaterFlowLayout){
            UIEdgeInsets insets = [collectionView.mk_collectionViewWaterFlowLayout insetForSectionAtIndex:sectionModel.indexInModel];
            size = UIEdgeInsetsInsetRect(bounds, insets).size;
        }
        return [view sizeThatFits:size];
    }];
}
@end


@interface TestItemViewsCollectionView : UIView<MKUICollectionViewDelegateWaterFlowLayout>
@property(nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,strong) TestItemsCollectionViewModel *model;
@property(nonatomic,strong) MKUICollectionViewWaterFlowLayout *waterCollectionLayout;
@property(nonatomic,copy) void(^whenSelectedChange)(TestItemViewsCollectionView *itemsView);
@property(nonatomic,copy) void(^whenDataChange)(TestItemViewsCollectionView *itemsView);
@end
@implementation TestItemViewsCollectionView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.waterCollectionLayout = [[MKUICollectionViewWaterFlowLayout alloc] init];
        self.collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:self.waterCollectionLayout];
        self.collectionView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
        
        self.waterCollectionLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.waterCollectionLayout.itemAlignment = MKCGRectAlignmentMid;
        self.waterCollectionLayout.sectionInset = UIEdgeInsetsMake(5, 10, 15, 20);
        self.waterCollectionLayout.interitemSpacing = 10;
        self.waterCollectionLayout.lineSpacing = 20;
        self.waterCollectionLayout.sectionSpacing = 5;
        
        MKUICollectionViewWaterFlowLayoutLastLineItemOption *option = [[MKUICollectionViewWaterFlowLayoutLastLineItemOption alloc] init];
        option.maxLines = 2;
        option.lastLineItemSize = CGSizeMake(44, 44);
        option.showLastLineItemWithinMaxLine = NO;
        self.waterCollectionLayout.lastLineItemReferenceOption = option;
        
        [self addSubview:self.collectionView];
        self.model = [[TestItemsCollectionViewModel alloc] initWithCollectionView:self.collectionView];
        self.model.allowsSelection = YES;
        self.model.forwardDelegate = self;
        @MK_WEAKIFY(self);
        self.model.whenDataChange = ^{
            @MK_NORMALIZE(self);
            if(self.whenDataChange){
                self.whenDataChange(self);
            }
        };
        self.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.layer.borderWidth = 1;
        
    }
    return self;
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize s = [self.waterCollectionLayout mk_sizeThatFits:size];
    return s;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    self.collectionView.frame = bounds;
    [self.collectionView reloadData];
}
- (void)removeAllItems{
    [self.model removeAllSectionModels];
    [self.collectionView reloadData];
    [self __notifyDataChange];
}
- (void)__notifyDataChange{
    if(self.whenDataChange){
        self.whenDataChange(self);
    }
}
- (void)addItemWithTitle:(NSString *)text animated:(BOOL)animated atFirst:(BOOL)first{
    Menu *menu = [[Menu alloc] init];
    menu.title = text;
    menu.iconName = [Menu random].iconName;
    MenuCollectionViewCellModel *cm = [[MenuCollectionViewCellModel alloc] initWithModelObject:menu];
    cm.cellClass = [TestItemCollectionViewCell class];
    @MK_WEAKIFY(self)
    cm.whenClick = ^(MenuCollectionViewCellModel * _Nonnull cm) {
        @MK_NORMALIZE(self)
        self.model.selectedCellModel = cm;
        self.model.selectedSectionModel = cm.sectionModel;
        if(self.whenSelectedChange){
            self.whenSelectedChange(self);
        }
    };
    TestItemsSectionModel *sm = self.model.selectedSectionModel;
    if(!sm){
        sm = self.model.sectionModels.lastObject;
    }
    if(!sm){
        sm = [[TestItemsSectionModel alloc] initWithHead:@"分组头部" foot:@"分组尾部"];
        sm.showLastLineItemWithinMaxLine = NO;
        sm.sectionInsets = UIEdgeInsetsZero;
        [self.model addSectionModel:sm];
    }
    if(first){
        [sm insertCellModel:cm atIndex:0];
    }else{
        [sm addCellModel:cm];
    }
    [self.collectionView reloadData];
    [self __notifyDataChange];
}
- (void)removeSelectItemWithAnimated:(BOOL)animated{
    MenuCollectionViewCellModel *selectedCellModel = self.model.selectedCellModel;
    if(!selectedCellModel) return;
    MKUICollectionViewSectionModel *sm = selectedCellModel.sectionModel;
    NSInteger index = selectedCellModel.indexInSectionModel;
    NSInteger sectionIndex = sm.indexInModel;
    [self.model removeCellModel:selectedCellModel];
    if(sm.numberOfCells>1){
        index = MIN(index,sm.numberOfCells-1);
    }
    self.model.selectedCellModel = [sm cellModelAtIndex:index];
    
    [[self.model sectionModelAtIndex:sectionIndex] refresh];
    
    if(self.whenSelectedChange){
        self.whenSelectedChange(self);
    }
    [self __notifyDataChange];
}
- (void)updateSelectItemWithTitle:(NSString *)text animated:(BOOL)animated{
    MenuCollectionViewCellModel *selectedCellModel = self.model.selectedCellModel;
    if(!selectedCellModel){
        return;
    }
    selectedCellModel.modelObject.title = text;
    [selectedCellModel.sectionModel refresh];
    
    if(self.whenSelectedChange){
        self.whenSelectedChange(self);
    }
    [self __notifyDataChange];
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView waterFlowLayout:(MKUICollectionViewWaterFlowLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    TestItemsSectionModel *sm = (TestItemsSectionModel *)[self.model sectionModelAtIndex:section];
    return sm.sectionInsets;
}
@end

@interface TestCollectionViewWaterFlowLayout ()<UITextFieldDelegate>
@property(nonatomic,strong) UITextField *inputField;
@property(nonatomic,strong) TestFunButton *addButton;
@property(nonatomic,strong) TestFunButton *cleanButton;
@property(nonatomic,strong) TestItemViewsCollectionView *itemsView;

@end

@implementation TestCollectionViewWaterFlowLayout

+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"瀑布流集合" viewControllerClass:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor mk_colorWithLight:[UIColor whiteColor]];
    self.inputField = [[UITextField alloc] init];
    self.inputField.borderStyle = UITextBorderStyleRoundedRect;
    self.inputField.delegate = self;
    [self.view addSubview:self.inputField];
    //
    self.addButton = [[TestFunButton alloc] initWithTitle:@"添加"];
    [self.addButton mk_addClickActionBlock:^(TestCollectionViewWaterFlowLayout *self) {
        [self _addText:self.inputField.text atFirst:YES];
    } context:self];
    [self.view addSubview:self.addButton];
    //
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_onViewTap:)]];
    
    //
    self.cleanButton = [[TestFunButton alloc] initWithTitle:@"清除"];
    [self.cleanButton mk_addClickActionBlock:^(TestCollectionViewWaterFlowLayout *self) {
        [self _cleanAll];
    } context:self];
    [self.view addSubview:self.cleanButton];
    //
    self.itemsView = [[TestItemViewsCollectionView alloc] init];
    @MK_WEAKIFY(self);
    self.itemsView.whenSelectedChange = ^(TestItemViewsCollectionView *itemsView) {
        @MK_NORMALIZE(self);
        if(itemsView.model.selectedCellModel){
            self.inputField.text = itemsView.model.selectedCellModel.modelObject.title;
            [self.addButton setTitle:@"修改" forState:(UIControlStateNormal)];
        }else{
            self.inputField.text = nil;
            [self.addButton setTitle:@"添加" forState:(UIControlStateNormal)];
        }
    };
    self.itemsView.whenDataChange = ^(TestItemViewsCollectionView *itemsView) {
        @MK_NORMALIZE(self);
        [self _onRefresh];
    };
    [self.view addSubview:self.itemsView];
    //
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemRefresh) target:self action:@selector(_onRefresh)];
    //
    [self _addText:@"Test1"];
    [self _addText:@"非常长的文本非常长的文本非常长的文本非常长的文本非常长的文本非常长的文本非常长的文本非常长的文本"];
    [self _addText:@"测试1"];
    [self _addText:@"测试2"];
    [self _addText:@"测试3"];
    [self _addText:@"测试4"];
    [self _addText:@"测试5"];
    [self _addText:@"测试6"];
    [self _addText:@"测试7"];
    [self _addText:@"测试8"];
    [self _addText:@"1"];
}

- (void)_onRefresh{
    [self.view setNeedsLayout];
    [self.itemsView.collectionView reloadData];
}
- (void)_onViewTap:(id)sender{
    [self.inputField endEditing:YES];
    self.itemsView.model.selectedCellModel = nil;
    
    if(self.itemsView.model.selectedCellModel){
        self.inputField.text = self.itemsView.model.selectedCellModel.modelObject.title;
        [self.addButton setTitle:@"修改" forState:(UIControlStateNormal)];
    }else{
        self.inputField.text = nil;
        [self.addButton setTitle:@"添加" forState:(UIControlStateNormal)];
    }
}
- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    CGRect bounds = self.safeBounds;
    
    MKUIFillingLayoutConstraint *l1 = [[MKUIFillingLayoutConstraint alloc] initWithItems:@[self.inputField,self.addButton,self.cleanButton] fillingItem:self.inputField constraintParam:(MKUIFillingLayoutConstraint_H_C) contentInsets:UIEdgeInsetsZero interitemSpacing:10];
    
    MKUILayoutConstraintItemWrapper *w2 = [MKUILayoutConstraintItemWrapper wrapItem:self.itemsView sizeThatFitsBlock:^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
//        return size;
        CGSize s2 = [wrapper.originItem sizeThatFits:size];
        return s2;
    }];
    
    MKUIFlowLayoutConstraint *flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[l1,w2] constraintParam:(MKUIFlowLayoutConstraintParam_V_T_L) contentInsets:UIEdgeInsetsZero interitemSpacing:10];
    flowlayout.bounds = bounds;
    [flowlayout layoutItemsWithResizeItems:YES];
    [self.itemsView setNeedsLayout];
}
- (void)_cleanAll{
    if(self.itemsView.model.selectedCellModel){
        [self.itemsView removeSelectItemWithAnimated:YES];
    }else{
        [self.itemsView removeAllItems];
    }
    [self _onRefresh];
}
- (void)_addText:(NSString *)text{
    [self _addText:text atFirst:NO];
}
- (void)_addText:(NSString *)text atFirst:(BOOL)atFirst{
    if(!text.length){
        return;
    }
    
    if(self.itemsView.model.selectedCellModel){
        [self.itemsView updateSelectItemWithTitle:text animated:YES];
    }else{
        [self.itemsView addItemWithTitle:text animated:YES atFirst:atFirst];
    }
    [self _onRefresh];
}

#pragma mark - delegate:UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self _addText:self.inputField.text];
    return YES;
}
@end
